import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../common/style/colors.dart';

class ImageView extends StatelessWidget {
  final String url;
  final BoxFit? fit;
  final double? width;
  final double? height;
  final Widget? placeholder;
  final BorderRadius? borderRadius;
  final double radius;

  const ImageView(
      {Key? key,
      required this.url,
      this.fit,
      this.width,
      this.height,
      this.placeholder,
      this.borderRadius,
      this.radius = 0.0})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final Color primaryColor = Theme.of(context).primaryColor;
    return borderRadius == null ? _image() : _radiusImage();
  }

  Widget _radiusImage() {
    return ClipRRect(
      borderRadius: borderRadius,
      child: _image(),
    );
  }

  Widget _image() {
    final isAssetsImage = url.startsWith('assets/');
    return isAssetsImage ? _assetsImage() : _netImage();
  }

  Widget _assetsImage() {
    return Image.asset(
      url,
      width: width,
      height: height,
      fit: fit,
    );
  }

  Widget _netImage() {
    final pla = placeholder ??
        const CupertinoActivityIndicator(
          color: AppColors.grey,
          radius: 18,
        );
    return CachedNetworkImage(
      width: width,
      height: height,
      fit: fit,
      imageUrl: url,

      progressIndicatorBuilder: (context, url, downloadProgress) {
        return pla;
      },
      // placeholder: (context, url) =>pla,
      errorWidget: (context, url, error) => const Icon(Icons.error),
    );
  }
}
